package com.orangelife.mobile.surrounding.biz;

import android.os.Handler;
import android.util.Log;

import com.common.sp.CommSp2;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.StringUtil;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.surrounding.entity.MerchantEntity;
import com.orangelife.mobile.surrounding.entity.MerchantTypeEntity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 周边商铺和商品-网络数据请求
 * Created by 绯若虚无 on 2016/1/18 0018.
 */
public class SurroundHttpBiz {
    /**
     * 商品列表
     */
    private static final String URL_CIRCUMTESO_HOTGOODS_LIST = Constant.URL_CIRCUMTESO_HOTGOODS_LIST2;
    /**
     * 商户列表
     */
    private static final String URL_MERCHANT_LIST = Constant.URL_MERCHANT_LIST;
    /**
     * 商户分类列表
     */
    private static final String URL_MERCHANT_CLASSIFY_LIST = Constant.URL_MERCHANT_CLASSIFY_LIST;


    /**
     * 获取商户列表
     */
    public static void getMerchantList(int pageNo, int pageSize, String orderBy, String industryConfigID, VolleyHttpUtil.ResponseCallBack callBack) {
        HashMap<String, String> map = new HashMap<>();
        map.put("pageIndex", String.valueOf(pageNo));
        map.put("pageSize", String.valueOf(pageSize));
        map.put("orderBy", orderBy);
        map.put("industryConfigID", industryConfigID);
        VolleyHttpUtil.getInstance().getWithCallback(URL_MERCHANT_LIST, map, callBack);
    }

    public static int getDataTotal(JSONObject response) {
        int total = 0;
        try {
            if (response != null) {
                String strTotal = response.getString("total");
                if (strTotal != null) {
                    try {
                        total = Integer.parseInt(strTotal);
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return total;
    }

    public static List<MerchantEntity> handleMerchantList(JSONObject response, int errCode) {
        List<MerchantEntity> list = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<MerchantEntity>>() {
                    }.getType(), null, null);
        }
        return list;
    }

    public static void getMerchantTypeList(VolleyHttpUtil.ResponseCallBack callBack) {
        VolleyHttpUtil.getInstance().getWithCallback(URL_MERCHANT_CLASSIFY_LIST, callBack);
    }

    public static List<MerchantTypeEntity> handleMerchantTypeList(JSONObject response, int errCode) {
        List<MerchantTypeEntity> list = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<MerchantTypeEntity>>() {
                    }.getType(), null, null);
        }
        return list;
    }

    /**
     * 获取商品列表
     *
     * @param pageNo   页码
     * @param pageSize 页数
     * @param handler  回调
     */
    public static void getGoodList(boolean hasNet, int pageNo, int pageSize, String merchantID, Handler handler, int what) {
        if (!hasNet && handler != null) {
            handler.sendEmptyMessage(-2);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("comID", CommSp2.getCommID());
        map.put("districtID", CommSp2.getDistrictID());
        if (!StringUtil.isBlank(merchantID)) {
            map.put("merchantID", merchantID);
        }

        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map", map);
        rootMap.put("url", URL_CIRCUMTESO_HOTGOODS_LIST);
        rootMap.put("wat", what);
        new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
    }

    public static void requestGoodsList(String merchantID, VolleyHttpUtil.ResponseCallBack callback) {

        HashMap<String, String> map = new HashMap<>();
        map.put("comID", CommSp2.getCommID());
        map.put("districtID", CommSp2.getDistrictID());
        if (!StringUtil.isBlank(merchantID)) {
            map.put("merchantID", merchantID);
        }
        VolleyHttpUtil.getInstance().getWithCallback(URL_CIRCUMTESO_HOTGOODS_LIST, map, callback);
    }

    public static int getGoodListTotal(Object response) {
        int total = 0;
        Map<String, Object> map = JSONHelper.jsonToMap(String.valueOf(response));
        if (map != null) {
            String strTotal = String.valueOf(map.get("total"));
            try {
                total = Integer.parseInt(strTotal);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            Log.e(SurroundHttpBiz.class.getSimpleName(), "total=" + strTotal);
        }
        return total;
    }

    public static List<Map<String, String>> getGoodListLeft(Object response) {
        Map<String, Object> map = JSONHelper.jsonToMap(String.valueOf(response));
        int count = 0;
        if (map != null) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("pageEntity");
            List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
            for (Map<String, Object> maps : list) {
                Map<String, String> leftMap = new HashMap<String, String>();

                leftMap.put("catName", maps.get("catName").toString());
                leftMap.put("count", count + "");
                listMap.add(leftMap);
                count += Integer.parseInt(maps.get("count").toString() + "");
            }
            return listMap;
        }
        return null;
    }


    public static List<Map<String, Object>> getGoodListRight(Object response) {
        Map<String, Object> map = JSONHelper.jsonToMap(String.valueOf(response));
        String goodsKey = "B";

        if (map != null) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("pageEntity");
            List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
            for (Map<String, Object> maps : list) {
                List<Map<String, Object>> goodslist = (List<Map<String, Object>>) maps.get("goods");
                goodsKey = goodsKey.equals("A") ? "B" : "A";
                for (Map<String, Object> goods : goodslist) {

                    Map<String, Object> rightMap = new HashMap<String, Object>();
                    rightMap = goods;
                    rightMap.put("key", goodsKey);
                    listMap.add(goods);
                }
            }
            return listMap;
        }
        return null;
    }

    public static List<List<Map<String, Object>>> handleGoodList(Object response) {
        Map<String, Object> map = JSONHelper.jsonToMap(String.valueOf(response));
        if (map != null) {
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("pageEntity");
            return dismanting(list);
        }
        return null;
    }

    private static List<List<Map<String, Object>>> dismanting(List<Map<String, Object>> listmap) {
        if (listmap != null) {
            List<Map<String, Object>> list1 = new ArrayList<>();
            List<List<Map<String, Object>>> list2 = new ArrayList<>();
            for (int i = 0; i < listmap.size(); i++) {
                list1.add(listmap.get(i));
                if (i % 2 > 0) {
                    list2.add(list1);
                    list1 = new ArrayList<>();
                }
                if (i == listmap.size() - 1 && i % 2 == 0) {
                    list2.add(list1);
                }
            }
            return list2;
        }
        return null;
    }
}
